$(document).ready(function () {

    $('#data-wrapper select').select0();

    $('#data-wrapper .btn-4-query').off('click').on('click', function () {
        var categoryLv1 = $('#data-wrapper select.categoryLv1').val();
        var categoryLv2 = $('#data-wrapper select.categoryLv2').val();
        if (categoryLv2) {
            $('#data-wrapper input[name=categoryId]').val(categoryLv2);
        } else if (categoryLv1) {
            $('#data-wrapper input[name=categoryId]').val(categoryLv1);
        } else {
            $('#data-wrapper input[name=categoryId]').val('');
        }
        ajaxSubmitResponseHtm({
            url: 'phase/query.htm',
            method: 'POST',
            data: $('#data-wrapper form').serialize()
        });
    });

    $('#data-wrapper .btn-4-add').off('click').on('click', function () {
        ajaxSubmitResponseHtm({
            container: '#pop-wrapper',
            url: 'phase/edit.htm'
        });
    });

    $('#data-wrapper .btn-4-modify').off('click').on('click', function () {
        var id = $(this).data('id');
        if (id) {
            ajaxSubmitResponseHtm({
                container: '#pop-wrapper',
                url: 'phase/edit.htm',
                data: 'id=' + id
            });
        }
    });

    $('#data-wrapper .btn-4-delete').off('click').on('click', function () {
        var id = $(this).data('id');
        if (id) {
            layer.confirm('确认删除？',
                {icon: 3, title: '删除提示', btn: ['确认', '取消']},
                function () {
                    ajaxSubmitResponseJSON({
                        url: 'phase/delete.htm/' + id,
                        success: function (data) {
                            if (data.status == 0) {
                                $('#data-wrapper .btn-4-query').trigger('click');
                            }
                        }
                    });
                }
            );
        }
    });

    $('#data-wrapper select.categoryLv1').off('change').on('change', function () {
        var categoryLv1Id = $(this).val();
        var htm = '<option value="">-请选择-</option>';
        if (!categoryLv1Id) {
            $('#data-wrapper select.categoryLv2').html(htm);
            $('#data-wrapper select.categoryLv2').select0();
            return;
        }
        ajaxSubmitResponseJSON({
            url: 'category/ajaxLevelTwoListByPid.htm/' + categoryLv1Id,
            success: function (data) {
                if (data.status == 0) {
                    var rowData = data.data;
                    if (rowData && rowData.length > 0) {
                        for (var i = 0; i < rowData.length; i++) {
                            htm += '<option value="' + rowData[i].id + '">' + rowData[i].name + '</option>';
                        }
                    }
                    $('#data-wrapper select.categoryLv2').html(htm);
                    $('#data-wrapper select.categoryLv2').select0();
                }
            }
        });
    });

    /*pageSize发生变化，从新加载页面数据*/
    $('#data-wrapper .paging-size-List').off('change').on('change', function () {
        var length = $(this).val();
        $('#data-wrapper form input[name="length"]').val(length);
        $('#data-wrapper .btn-4-query').trigger('click');
    });

    /*点击分页按钮，实现翻页功能*/
    $('#data-wrapper .pagination-toolbar a').off('click').on('click', function () {
        var distStart = $(this).data('idx');
        var start = $('#data-wrapper form input[name="start"]').val();
        if (start != distStart) {
            $('#data-wrapper form input[name="start"]').val(distStart);
            $('#data-wrapper .btn-4-query').trigger('click');
        }
    });
});